package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.ItemShop;
import cn.wolfcode.luowowo.article.query.ItemShopQuery;
import cn.wolfcode.luowowo.article.service.IItemShopService;
import cn.wolfcode.luowowo.comment.domain.ItemShopComment;
import cn.wolfcode.luowowo.comment.domain.UserGoods;
import cn.wolfcode.luowowo.comment.query.ItemShopCommentQuery;
import cn.wolfcode.luowowo.comment.query.UserGoodsQuery;
import cn.wolfcode.luowowo.comment.service.IItemShopCommentService;
import cn.wolfcode.luowowo.comment.service.IUserGoodsService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.domain.UserInfoScore;
import cn.wolfcode.luowowo.member.service.IUserInfoScoreService;
import cn.wolfcode.luowowo.website.annoation.UserParam;
import cn.wolfcoe.luowowo.common.util.baiducheck.SensitiveWordsUtil;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * 积分商城控制器
 */
@Controller
@RequestMapping("itemshop")
public class ItemshopController {
    //操作商品
    @Reference
    private IItemShopService iItemShopService;
    //操作用户积分
    @Reference
    private IUserInfoScoreService userInfoScoreService;
    //操作用户兑换的商品
    @Reference
    private IUserGoodsService userGoodsService;
    //操作留言板
    @Reference
    private IItemShopCommentService itemShopCommentService;

    //列表页面
    @RequestMapping("list")
    public String index(Model model, @UserParam UserInfo userInfo) {

        //获取用户的id查询用户的积分
        if (userInfo == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("score", "<a id=\"loginGo\">请登录</a>");
            model.addAttribute("userInfo", map);
            return "itemshop/list";
        }
        UserInfoScore userInfoScore = userInfoScoreService.get(userInfo.getId());
        model.addAttribute("userInfo", userInfoScore);
        return "itemshop/list";
    }

    //查询商城商品和用户已兑换的商品
    @RequestMapping("query")
    public String query(Model model, ItemShopQuery qo, @UserParam UserInfo userInfo, Long queryType) {
        //queryType==1是查询用户兑换的商品
        if (queryType != null && queryType == 1L && userInfo != null) {
            //获取用户的id设置到Query查询条件
            UserGoodsQuery userGoodsQuery = new UserGoodsQuery();
            userGoodsQuery.setUserId(userInfo.getId());
            //查询数据库获取给用户的所有已兑换商品
            Page<UserGoods> query = userGoodsService.query(userGoodsQuery);
            model.addAttribute("pageInfo", query);
            return "itemshop/myItemTempl";
        } else {
            //查询商城商品
            model.addAttribute("pageInfo", iItemShopService.query(qo));
            return "itemshop/itemTempl";
        }
    }

    @RequestMapping("buy")
    @ResponseBody
    public Object buy(Model model, ItemShop itemShop, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return new AjaxResult(false, "请先登陆");
        }
        //修改商品剩余数量
        AjaxResult json = iItemShopService.reduce(userInfo,itemShop);
        if (!json.isSuccess()) {
            return json;
        }
        //用户兑换商品消费积分
         json = userInfoScoreService.expenditure(userInfo, itemShop.getPrice().intValue());
        if (!json.isSuccess()) {
            return json;
        }
        //添加用户兑换商品记录
        userGoodsService.cashGift(userInfo.getId(), itemShop);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("coin")
    @ResponseBody
    public Object coin(Model model, @UserParam UserInfo userInfo, String score) {
        if (userInfo == null) {
            return new AjaxResult(false, "请先登陆");
        }
        //增加了用户积分
        userInfoScoreService.addscore(userInfo, Integer.valueOf(score));
        return AjaxResult.SUCCESS;
    }

    //comment
    @RequestMapping("comment")
    public Object comment(Model model, @UserParam UserInfo userInfo, ItemShopComment comment, ItemShopCommentQuery qo) {

        if (comment.getContent() != null && comment.getContent() != "" && userInfo != null) {
            comment.setFromId(userInfo.getId());
            comment.setCity(userInfo.getCity());
            comment.setFromImg(userInfo.getHeadImgUrl());
            comment.setFromLevel(userInfo.getLevel());
            comment.setFromName(userInfo.getNickname());
            comment.setContent(SensitiveWordsUtil.SensitiveWordCheck(comment.getContent()).getContent());
            itemShopCommentService.saveOrUpdate(comment);
        }

        Page<ItemShopComment> page = itemShopCommentService.query(qo);
        model.addAttribute("page", page);
        return "itemshop/commentTempl";
    }
}
